import { createRouter, createWebHashHistory } from "vue-router";
import { showSuccessToast, showFailToast } from "vant";
// import { useSignInStore } from "../stores/eid.js";
// import pinia from "../stores/index";

// const stores = useSignInStore(pinia);

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: "/",
      name: "home",
      redirect: "/Home",
      component: () => import("../views/Home.vue"),
      children: [
        {
          path: "/Shop",
          name: "Shop",
          meta: {
            title: "商品",
          },
          component: () => import("../views/Shop.vue"),
        },
        {
          path: "/My",
          name: "My",
          meta: {
            title: "我的",
          },
          component: () => import("../views/My.vue"),
          children: [],
        },
        {
          path: "/Map",
          name: "Map",
          meta: {
            title: "地图",
          },

          component: () => import("../views/Map.vue"),
        },
        {
          path: "/Home",
          name: "HomeSon",
          meta: {
            title: "主页",
          },
          component: () => import("../views/HomeSon.vue"),
        },
      ],
    },

    {
      path: "/Login",
      name: "Login",
      meta: {
        title: "登录",
      },
      component: () => import("../views/Login.vue"),
    },
    {
      path: "/:pathMatch(.*)*",
      component: () => import("../views/404.vue"),
      meta: {
        title: "404",
      },
    },
    {
      path: "/Classified",
      name: "Classified",
      meta: {
        title: "分类",
      },
      component: () => import("../views/Home/Classified.vue"),
    },
    {
      path: "/Classify",
      name: "Classify",
      meta: {
        title: "商品分类",
      },
      component: () => import("../views/Shop/Classify.vue"),
    },
    {
      path: "/Search",
      name: "Search",
      meta: {
        title: "搜索",
      },
      component: () => import("../components/Search.vue"),
    },
    {
      path: "/ShopDetail",
      name: "ShopDetail",
      meta: {
        title: "详情",
      },
      component: () => import("../views/Shop/Detail.vue"),
    },
    {
      path: "/OrderSucess",
      name: "OrderSucess",
      meta: {
        title: "支付成功",
      },
      component: () => import("../views/Shop/OrderSucess.vue"),
    },
    {
      path: "/FinishDetail",
      name: "FinishDetail",
      meta: {
        title: "订单详情已完成支付",
      },
      component: () => import("../views/Shop/FinishDetail.vue"),
    },
    {
      path: "/DistributionDetail",
      name: "DistributionDetail",
      meta: {
        title: "订单详情配送中",
      },
      component: () => import("../views/Shop/DistributionDetail.vue"),
    },
    {
      path: "/WaitingDetail",
      name: "WaitingDetail",
      meta: {
        title: "订单详情等待取货中",
      },
      component: () => import("../views/Shop/WaitingDetail.vue"),
    },
    {
      path: "/Delivery",
      name: "Delivery",
      meta: {
        title: "订单详情配送货中",
      },
      component: () => import("../views/My/Delivery.vue"),
    },
    {
      path: "/ShopList",
      name: "ShopList",
      redirect: "/ListSon",
      meta: {
        title: "商品列表",
      },
      component: () => import("../views/Shop/List.vue"),
      children: [
        {
          path: "/ListSon",
          name: "ListSon",
          component: () => import("../views/Shop/ListSon.vue"),
        },
      ],
    },
    {
      path: "/Recallorder",
      name: "Recallorder",
      meta: { title: "回收订单" },
      component: () => import("../views/My/Recallorder.vue"),
    },
    {
      path: "/Mycollect",
      name: "Mycollect",
      meta: { title: "我的收藏" },
      component: () => import("../views/My/Mycollect.vue"),
    },
    {
      path: "/Myappraise",
      name: "Myappraise",
      meta: { title: "评价中心" },
      component: () => import("../views/My/Myappraise.vue"),
    },
    {
      path: "/Callreceivingclerk",
      name: "Callreceivingclerk",
      meta: { title: "致电" },
      component: () => import("../views/My/Callreceivingclerk.vue"),
    },
    {
      path: "/Orderofexchange",
      name: "Orderofexchange",
      meta: { title: "兑换订单old" },
      component: () => import("../views/My/Orderofexchange.vue"),
    },
    {
      path: "/OrderList",
      name: "OrderList",
      meta: { title: "兑换订单详情" },
      component: () => import("../views/My/OrderList.vue"),
    },
    {
      path: "/Shipping",
      name: "Shipping",
      meta: { title: "订单配送中" },
      component: () => import("../views/My/Shipping.vue"),

      path: "/Waitorderreceiving",
      name: "Waitorderreceiving",
      meta: { title: "等待接单" },
      component: () =>
        import("../views/My/Mycomponents/Waitorderreceiving.vue"),
    },
    {
      path: "/Withdrawdeposit",
      name: "Withdrawdeposit",
      meta: { title: "提现" },
      component: () => import("../views/My/Withdrawdeposit.vue"),
    },
    {
      path: "/Mymsg",
      name: "Mymsg",
      meta: { title: "消息中心" },
      component: () => import("../views/My/Mymsg.vue"),
    },
    {
      path: "/Waitorderreceiving",
      name: "Waitorderreceiving",
      meta: { title: "等待接单" },
      component: () =>
        import("../views/My/Mycomponents/Waitorderreceiving.vue"),
    },
    {
      path: "/Withdrawdeposit",
      name: "Withdrawdeposit",
      meta: { title: "提现" },
      component: () => import("../views/My/Withdrawdeposit.vue"),
    },
    {
      path: "/Mymsg",
      name: "Mymsg",
      meta: { title: "消息中心" },
      component: () => import("../views/My/Mymsg.vue"),
    },
    {
      path: "/Shipping",
      name: "Shipping",
      meta: { title: "订单配送中" },
      component: () => import("../views/My/Shipping.vue"),
    },
    {
      path: "/Completed",
      name: "Completed",
      meta: { title: "订单已完成" },
      component: () => import("../views/My/Completed.vue"),
    },
    {
      path: "/Myfootprint",
      name: "Myfootprint",
      meta: { title: "我的足迹" },
      component: () => import("../views/My/Myfootprint.vue"),
    },
    {
      path: "/Mycontribution",
      name: "Mycontribution",
      meta: { title: "我的贡献" },
      component: () => import("../views/My/Mycontribution.vue"),
    },
    {
      path: "/MyIE",
      name: "MyIE",
      meta: { title: "收支明细" },
      component: () => import("../views/My/MyIE.vue"),
    },
    {
      path: "/SearchGoods",
      name: "SearchGoods",
      meta: {
        title: "搜索详情",
      },
      component: () => import("../components/SearchGoods.vue"),
    },
    {
      path: "/SearchNoGoods",
      name: "SearchNoGoods",
      meta: {
        title: "没有搜索到详情",
      },
      component: () => import("../components/SearchNoGoods.vue"),
    },
    // 单个商品解释页面
    {
      path: "/GarbageDetails",
      name: "GarbageDetails",
      meta: {
        title: "回收解释页",
      },
      component: () => import("../components/GarbageDetails.vue"),
    },
    {
      path: "/Empty",
      name: "Empty",
      meta: {
        title: "购物车",
      },
      component: () => import("../views/Shop/Empty.vue"),
    },
    {
      path: "/Car",
      name: "Car",
      meta: {
        title: "购物车",
      },

      component: () => import("../views/Shop/Car.vue"),
    },
    {
      path: "/calendar",
      name: "calendar",
      meta: {
        title: "投放日历",
      },
      component: () => import("../views/Home/calendar.vue"),
    },
    {
      path: "/everyday",
      name: "everyday",
      meta: {
        title: "每日签到",
      },
      component: () => import("../views/Home/everyday.vue"),
    },

    {
      path: "/Distinguish",
      name: "Distinguish",
      meta: {
        title: "垃圾识别",
      },
      component: () => import("../views/Home/Distinguish.vue"),
    },
    {
      path: "/Recovery",
      name: "Recovery",
      meta: {
        title: "上门回收",
      },
      component: () => import("../views/Home/Recovery.vue"),
    },
    // 跳转我的设置页面
    {
      path: "/SetUp",
      name: "SetUp",
      meta: {
        title: "设置",
      },
      component: () => import("../views/My/SetUp.vue"),
    },
    // 责任声明
    {
      path: "/AboutDuty",
      name: "AboutDuty",
      meta: {
        title: "关于",
      },
      component: () => import("../views/Set/AboutDuty.vue"),
    },
    // 支付密码
    {
      path: "/ChangePassword",
      name: "ChangePassword",
      meta: {
        title: "支付密码修改",
      },
      component: () => import("../views/Set/ChangePassword.vue"),
    },
    // 手机号修改
    {
      path: "/ChangePhone",
      name: "ChangePhone",
      meta: {
        title: "手机号修改",
      },
      component: () => import("../views/Set/ChangePhone.vue"),
    },
    // 消息提示
    {
      path: "/Message",
      name: "Message",
      meta: {
        title: "消息提示",
      },
      component: () => import("../views/Set/Message.vue"),
    },
    // 意见反馈
    {
      path: "/Opinion",
      name: "Opinion",
      meta: {
        title: "意见反馈",
      },
      component: () => import("../views/Set/Opinion.vue"),
    },
    {
      path: "/SetHeadImg",
      name: "SetHeadImg",
      meta: {
        title: "更改头像",
      },
      component: () => import("../views/Set/SetHeadImg.vue"),
    },
  ],
});

router.beforeEach((to, from, next) => {
  if (to.meta) {
    document.title = to.meta.title;
  }
  const token = localStorage.getItem("token");
  if (
    !token &&
    (to.path == "/My" ||
      to.path == "/everyday" ||
      to.path == "/Empty" ||
      to.path == "/Car")
  ) {
    showFailToast("请您先登录！！！");
    return next("/login");
  }
  if (token && to.path == "/login") {
    showFailToast("请勿重复登录！！！");
    return next({
      path: from.path ? from.path : "/login",
    });
  }
  next();
});


router.afterEach(()=>{
  window.scrollTo(0,0)
})

export default router;
